/**
 * Copyright 2009 by Christian Scheid. This file is part of the switch eclipse plugin and is licensed under the GPL
 * version 3. Please refer to the URL http://www.gnu.org/licenses/gpl-3.0.html for details.
 */
package org.digitalsoul.switchplugin.model;

import org.eclipse.core.resources.IFile;
import org.eclipse.core.resources.IProject;
import org.eclipse.core.resources.IResource;

public class SwitchFile implements ISwitchFile {

    protected IFile file;
    private String baseName;
    private String fileExtension = "";
    private String location;

    public SwitchFile(ISwitchFile view) {
        this(view.getIFile());
    }

    /**
     * @param file
     */
    public SwitchFile(IFile file) {
        this.file = file;
        resetBaseNameAndExtension(file.getName());
        location = getLocation();
    }

    private void resetBaseNameAndExtension(String name) {
        String[] parts = name.split("\\.");
        String extension = "";
        if (parts != null && parts.length > 1) {
            String ext = parts[parts.length - 1];
            if (ext.trim().length() > 0) {
                extension = ext;
            }
            baseName = "";
            for (int i = 0; i < parts.length - 1; i++) {
                if (baseName.length() > 0) {
                    baseName += ".";
                }
                baseName += parts[i];
            }
        }
        else {
            baseName = name;
        }
        fileExtension = extension;
    }

    /**
     * @return <p>
     *         the full name including the extension
     *         </p>
     *         e.g <tt>README.txt</tt>
     */
    public String getName() {
        if (fileExtension.length() > 0) {
            return baseName + "." + fileExtension;
        }
        else return baseName;
    }

    /**
     * @return <p>
     *         the name excluding the extension
     *         </p>
     *         e.g <tt>README</tt>
     */
    public String getBaseName() {
        return baseName;
    }

    /**
     * @return
     */
    public String getFileExtension() {
        return fileExtension;
    }

    /**
     * @see java.lang.Object#equals(java.lang.Object)
     */
    @Override
    public boolean equals(Object obj) {
        ISwitchFile other = (ISwitchFile) obj;
        boolean sameLocation = other.getLocation().equals(getLocation());
        boolean samePath = other.getPath().equals(getPath());
        boolean sameName = other.getBaseName().equals(getBaseName());
        boolean sameExtension = other.getFileExtension().equals(getFileExtension());
        return sameLocation && samePath && sameName && sameExtension;
    }

    /**
     * 
     */
    protected void resetFile() {
        IProject project = file.getProject();
        this.file = project.getFile(location + "/" + getName());
    }

    /**
     * @param baseName
     */
    public void changeBaseName(String baseName) {
        this.baseName = baseName;
        resetFile();
    }

    /**
     * Returns the project relative location of the file in the project tree excluding the file itself e.g
     * <tt>src/main/java/org/foo</tt>
     * 
     * @return the project relative location of the file
     */
    public String getLocation() {
        IResource parent = file.getParent();
        return parent.getProjectRelativePath().toString();
    }

    public void changeName(String newName) {
        resetBaseNameAndExtension(newName);
        resetFile();
    }

    public void changePath(String path) {
        int index = path.lastIndexOf("/");
        String filename = path.substring(index + 1, path.length());
        resetBaseNameAndExtension(filename);
        String location = path.substring(0, index);
        changeLocation(location);
    }

    /**
     * Changes the location of this SwitchFile instance and returns the instance for convenience
     * 
     * @param location the new location to set
     */
    public ISwitchFile changeLocation(String location) {
        this.location = location;
        resetFile();
        return this;
    }

    /**
     * @param fileExtension
     */
    public void changeFileExtension(String fileExtension) {
        this.fileExtension = fileExtension;
        resetFile();
    }

    /**
     * Returns the project relative path of the file in the project tree e.g src/main/java/org/foo/Test.java
     * 
     * @return the project relative location of the file
     */
    public String getPath() {
        return file.getProjectRelativePath().toString();
    }

    /**
     * @return
     */
    public boolean exists() {
        return file.exists();
    }

    /**
     * @return
     */
    public ISwitchFile makeCopy() {
        return new SwitchFile(file);
    }

    /**
     * @return
     */
    public IFile getIFile() {
        return file;
    }

    public IProject getProject() {
        return file.getProject();
    }

    @Override
    public String toString() {
        return getPath();
    }
}
